#!/bin/bash

result=`sed -f sedscript.sed data.txt`
echo $result
echo ""
sed -f sedscript.sed data.txt


echo "====="
echo ""

sed '3a\
This is an appended line.\
' data6.txt

# 输出：
# This is line number 1.
# This is line number 2.
# This is line number 3.
# This is an appended line.
# This is line number 4.

echo ""
sed '3i\
This is an appended line.\
This is an appended line2.\
' data6.txt

# 输出：
# This is line number 1.
# This is line number 2.
# This is an appended line.
# This is an appended line2.
# This is line number 3.
# This is line number 4.

echo "TODO"
sed -e '3a\
This is an appended line.\/; s/a/a/w data6_append.txt' data6.txt

# 回顾打印
echo ""
echo "p 回顾打印"
# 第三行把line替换为test，
# -n 不打印没有修改的行
# p 打印旧行
# s/line/test/p 打印替换之后的行
sed -n '/3/{
	p
	s/line/test/p
	}' data6.txt

# 输出：
# This is line number 3.
# This is test number 3.


echo ""
echo "处理文本文件"
# 处理文本文件
# 替换 notice.std 文件中的 ”LIST“ 字符串为 data11.txt 文件内容
# r data11.txt 读取data11.txt文件内容添加到文件中 ”LIST“ 位置
# d 删除”LIST“
sed '/LIST/{ 
	r data11.txt
	d
	}' notice.std

# notice.std 文件内容
# Would the following people:
# LIST
# please report to the ship's captain.

# data11.txt文件内容
# Blum, R Browncoat
# McGuiness, A Alliance
# Bresnahan, C Browncoat
# Harken, C Alliance

# 输出：
# Would the following people:
# Blum, R Browncoat
# McGuiness, A Alliance
# Bresnahan, C Browncoat
# Harken, C Allianceplease report to the ship's captain.

echo ""
echo "多行版本的next命令（用大写N）"
echo "原始"
cat data3.txt
echo ""
echo "替换之后"
# 多行版本的next命令（用大写N）会将下一文本行添加到模式空间中已有的文本后。
sed 'N
	s/System\nAdministrator/Desktop\nUser/
	s/System Administrator/Desktop User/
	' data3.txt

echo ""
echo "====="
sed '{
	s/System Administrator/Desktop User/
	N
	s/System\nAdministrator/Desktop\nUser/
}' data3.txt

echo ""
echo "删除（D）"
sed '{
	N
	/System\nAdministrator/D
}' data3.txt

echo ""
echo "原始数据"
cat data5.txt

echo ""
echo "删除第一个空白行（D）"
sed '/^$/{
	N
	/one/D
}' data5.txt


echo ""
echo ""

sed '{$!N;
	s/System\nAdministrator/Desktop\nUser/
	s/System Administrator/Desktop User/
}' data3.txt


echo ""
echo ""
echo "分支跳过2到3行  2,3b"
sed '{2,3b ; s/This is/Is this/ ; s/line./test?/}' data2.txt

echo ""
echo ""
echo "分支跳过jump1定义的位置  /first/b jump1"
sed '{/first/b jump1 ; s/This is the/No jump on/
	:jump1
	s/This is the/Jump here on/
}' data2.txt


echo ""
echo ""
echo "分支跳过jump1定义的位置  /first/b jump1"
sed '{/first/b jump1 ; s/This is the/No jump on/
	:jump1
}' data2.txt


# echo ""
# echo ""
# echo "This, is, a, test, to, remove, commas." | sed -n '{
# 	:start
# 	s/,//1p
# 	b start
# }'


echo ""
echo ""
echo "循环替换出现的 ”,“ 为空"
echo "This, is, a, test, to, remove, commas."

# :start 标记
# s/,//1 替换第一处出现的”,“为空
# p 打印此次替换的结果
# /,/b start 如果匹配到/,/模式的文本跳转到:start 标记处执行，类似于一个循环
echo "This, is, a, test, to, remove, commas." | sed -n '{
	:start
	s/,//1
	p
	/,/b start
}'

# 结果：
# This, is, a, test, to, remove, commas.
# This is, a, test, to, remove, commas.
# This is a, test, to, remove, commas.
# This is a test, to, remove, commas.
# This is a test to, remove, commas.
# This is a test to remove, commas.
# This is a test to remove commas.

echo ""
echo "This, is, a, test, to, remove, commas." | sed -n '{
	:start
	s/,//1
	/,/b start
	p
}'


echo ""
echo "在数字中添加逗号分隔"
echo "1234567" | sed '{
	:start
	s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
	t start
}'



